<template>
  <div class="file-tree-node">
    <div :style="indent" class="file-tree-node-name"
      :class="fullName === selectedFile ? 'highlight' : ''"
      @click="toggle($event, node)"
      @contextmenu="onContextMenu($event, node)">
      <div class="node-icon" :class="node.children ? showChildren ? 'node-icon-down' : 'node-icon-right' : 'node-icon-file'"></div>
      <div class="node-name">{{ node.name }}</div>
    </div>
    <file-tree-node v-show="showChildren" v-for="node in children"
    :node="node"
    :children="node.children"
    :key="node.name" :depth="depth + 1">
    </file-tree-node>
  </div>
</template>
<script>
import { mapState } from 'vuex'
import { FILE_TYPE, OPERATION_TYPE } from '../type'
export default {
  data () {
    return {
      showChildren: false
    }
  },
  name: 'file-tree-node',
  props: ['node', 'children', 'depth'],
  computed: {
    ...mapState(['selectedFile']),
    indent () {
      return { paddingLeft: `${this.depth * 25 + 10}px` }
    },
    fullName () {
      const nameList = []
      let node = this.node
      while (node.parent) {
        nameList.unshift(node.name)
        node = node.parent
      }
      return '/' + nameList.join('/')
    }
  },
  methods: {
    toggle (e, node) {
      if (node.type === FILE_TYPE.DIR) {
        this.showChildren = !this.showChildren
      } else if (node.type === FILE_TYPE.FILE) {
        this.$store.dispatch('selectFile', this.fullName)
        this.$store.dispatch('readFileSync', this.fullName)
      }
    },
    onContextMenu (event, item) {
      event.preventDefault()
      event.stopPropagation()
      const pos = {
        x: event.x || event.pageX,
        y: event.y || event.pageY,
        show: true
      }
      this.$store.dispatch('changeMenuStatus', pos)
      let list
      if (item.type === FILE_TYPE.FILE) {
        list = [{
          desc: OPERATION_TYPE.RENAME_FILE.desc,
          dirtype: OPERATION_TYPE.RENAME_FILE.dirtype,
          name: item.name,
          fullName: this.fullName
        }, {
          desc: OPERATION_TYPE.DELETE_FILE.desc,
          dirtype: OPERATION_TYPE.DELETE_FILE.dirtype,
          name: item.name,
          fullName: this.fullName
        }]
      }
      if (item.type === FILE_TYPE.DIR) {
        list = [{
          desc: OPERATION_TYPE.CREATE_FILE.desc,
          dirtype: OPERATION_TYPE.CREATE_FILE.dirtype,
          name: item.name,
          fullName: this.fullName
        }, {
          desc: OPERATION_TYPE.CREATE_DIRECTORY.desc,
          dirtype: OPERATION_TYPE.CREATE_DIRECTORY.dirtype,
          name: item.name,
          fullName: this.fullName
        }, {
          desc: OPERATION_TYPE.RENAME_FILE.desc,
          dirtype: OPERATION_TYPE.RENAME_FILE.dirtype,
          name: item.name,
          fullName: this.fullName
        }]
        if (item.parent.parent) {
          list.push({
            desc: OPERATION_TYPE.DELETE_DIRECTORY.desc,
            dirtype: OPERATION_TYPE.DELETE_DIRECTORY.dirtype,
            name: item.name,
            fullName: this.fullName
          })
        }
      }
      this.$store.dispatch('changeMenuList', list)
    }
  }
}
</script>
<style scoped>
  .file-tree-node-name {
    user-select: none;
    display: flex;
    height: 40px;
    line-height: 40px;
    padding-left: 10px;
    cursor: pointer;
  }
  .file-tree-node-name:hover {
    background: #666;
  }
  .node-icon {
    width: 20px;
    height: 20px;
    margin-top: 10px;
    background-size: cover;
  }
  .node-icon-right {
    background-image: url('../assets/imgs/arrow-right.png');
  }
  .node-icon-down {
    background-image: url('../assets/imgs/arrow-down.png');
  }
  .node-icon-file {
    background-image: url('../assets/imgs/file.png');
  }
  .node-name {
    padding: 0 10px;
  }
  .highlight {
    background: #444;
  }
</style>
